package com.reactim.message.config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import com.reactim.message.connection.ConnectionDetails;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;

/**
 * Caffeine 缓存配置
 */
@Configuration
public class CaffeineConfig {

    /**
     * 连接详情缓存
     */
    @Bean("connectionDetailsCache")
    public Cache<Long, ConnectionDetails> connectionDetailsCache() {
        return Caffeine.newBuilder()
                .maximumSize(10000)
                .expireAfterWrite(Duration.ofMinutes(30))
                .build();
    }

    /**
     * 在线状态缓存
     */
    @Bean("onlineStatusCache")
    public Cache<Long, Boolean> onlineStatusCache() {
        return Caffeine.newBuilder()
                .maximumSize(20000)
                .expireAfterWrite(Duration.ofMinutes(5))
                .build();
    }

    /**
     * 统计信息缓存
     */
    @Bean("statsCache")
    public Cache<String, Long> statsCache() {
        return Caffeine.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(Duration.ofMinutes(10))
                .build();
    }
}